import { _decorator, Component, game, instantiate, Node, Prefab } from 'cc';
import { HttpCom } from '../net/HttpCom';
import { GameEvent } from '../EventName';
import { SignInData, UserSignInData } from '../commonComp/InterfaceCom';
import { SignIn } from '../SignIn';
import { SignInItem } from '../SignInItem';
import { TaskItem } from '../TaskItem';
const { ccclass, property } = _decorator;

@ccclass('TaskPage')
export class TaskPage extends Component {

    @property(Node)
    signItemContent: Node = null;
    @property(Node)
    taskContent: Node = null;

    @property(Prefab)
    taskItem: Prefab = null;



    protected onLoad(): void {
        game.on(GameEvent.ON_SIGNDATA_UPDATE,this.ON_SIGNDATA_UPDATE,this);
        game.on(GameEvent.ON_USER_SIGNDATA_UPDATE,this.ON_USER_SIGNDATA_UPDATE,this);
        game.on(GameEvent.ON_USER_SIGN_RESPONE,this.ON_USER_SIGN_RESPONE,this);
        game.on(GameEvent.ON_CHECK_IS_SIGNED,this.ON_CHECK_IS_SIGNED,this);
    }

    start() {
        //HttpCom.signinList();
    }

    ON_SIGNDATA_UPDATE(data?: SignInData[]){
        let items = this.signItemContent.children;
        data.forEach((e,i)=>{
            let val = JSON.parse(e.value);
            console.log(val)
            let com = items[i].getComponent(SignInItem);
            
            com.initView(val,e.name);
            
        })
    }
    ON_USER_SIGNDATA_UPDATE(data?: UserSignInData){
        let items = this.signItemContent.children;
        let rows = data.rows;

        rows.forEach((e,i)=>{
            items[i].getComponent(SignInItem).changeSignState(true);
        })

    }
    ON_USER_SIGN_RESPONE(data?: SignIn){

    }
    ON_CHECK_IS_SIGNED(data?: any){
        let items = this.signItemContent.children;
        if(data.data){

        }else{

            for(let i = 0 ; i < items.length ; i++){
                let com = items[i].getComponent(SignInItem);
                if(com.bg.grayscale == true){
                    com.bg.grayscale = false;
                    com.signBtn.interactable = true;
                    return;
                }
            }
           
        }
    }

    updateTaskList(resp){
        this.taskContent.removeAllChildren();
        let rows = resp.rows;
        try{

            rows.forEach((e,i)=>{
                let reward = JSON.parse(e.value)[0]
                let item = instantiate(this.taskItem);
                let com = item.getComponent(TaskItem);
                let iconIndex = 0;
                let rewardIconIdex = 0;
                if(e.id == 1){
                    iconIndex = 0;
                    
                }
                if(e.id == 3 || e.id == 6){
                    iconIndex = 1;
                }
                
                if(reward.name){
    
                    if(reward.name.toLowerCase() == 'gold'){
                        rewardIconIdex = 0;
                    }
                    if(reward.name.toLowerCase() == 'ticket'){
                        rewardIconIdex = 1;
                    }
                }
                
                this.taskContent.addChild(item);
                com.initView(iconIndex,e.name,rewardIconIdex,reward,e.id)
            })
        }

        catch(e){

        }
        
    }

    updateTaskState(resp){
        let rows = resp.rows;
        if(rows.length == 0){
            //this.taskContent.children[0].getComponent(TaskItem).changeFineshState(true);
        }

        this.taskContent.children.forEach((e,i)=>{
            let com = e.getComponent(TaskItem);
            if(com.taskId == 6){
                HttpCom.isLikeX(com.taskId,(resp)=>{
            
                    com.changeFineshState(resp.data);
                    
    
                    
                })
            }
        })
        rows.forEach((e,i)=>{
            let taskId = e.taskId;
            this.taskContent.children.forEach((ee,ii)=>{
                let com = ee.getComponent(TaskItem);
                if(com.taskId == taskId){
                    com.changeFineshState(true);
                }

                
            })
        })
    }

    protected onEnable(): void {
        HttpCom.signinList();
        HttpCom.taskList((resp)=>{
            this.updateTaskList(resp)
            HttpCom.userTaskList((resp)=>{
                this.updateTaskState(resp)
            })
        })
        //HttpCom.userSigninList()
    }

    update(deltaTime: number) {
        
    }
}


